/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DialogCadOrcamento.java
 *
 * Created on 11/04/2011, 10:26:53
 */
package camada.apresentacao.dialog.orcamento;

import camada.bancodedados.daoimplemente.cliente.PessoaFisicaDAOImpl;
import camada.bancodedados.daoimplemente.FormaPagamentoDAOImpl;
import camada.bancodedados.daoimplemente.orcamento.MaterialProdutoOrcamentoDAOImpl;
import camada.bancodedados.daoimplemente.orcamento.OrcamentoDAOImpl;
import camada.bancodedados.daoimplemente.orcamento.PagamentoDAOImpl;
import camada.bancodedados.daoimplemente.orcamento.ProdutoOrcamentoDAOImpl;
import camada.bancodedados.daoimplemente.orcamento.TipoProdutoDAOImpl;
import combomodel.ComboBoxClienteModel;
import combomodel.ComboBoxFormaPagamentoModel;
import eloma.classe.Cliente;
import eloma.classe.FormaPagamento;
import eloma.classe.Pagamento;
import eloma.classe.orcamento.MaterialProdutoOrcamento;
import eloma.classe.orcamento.Orcamento;
import eloma.classe.orcamento.ProdutoOrcamento;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import tabelamodelcolumnmodel.orcamento.ProdutoOrcamentoColumnModel;
import tabelamodelcolumnmodel.orcamento.ProdutoOrcamentoTableModel;
import utilitarios.Exceptions;
import utilitarios.LimitarTamanhoTextField;

/**
 *
 * @author icaro.klinger
 */
public class DialogCadOrcamento extends javax.swing.JDialog {

    PessoaFisicaDAOImpl daoCliente = new PessoaFisicaDAOImpl();
    OrcamentoDAOImpl daoOrcamento = new OrcamentoDAOImpl();
    ProdutoOrcamentoDAOImpl daoProdutoOrcamento = new ProdutoOrcamentoDAOImpl();
    MaterialProdutoOrcamentoDAOImpl daoMaterialProduto = new MaterialProdutoOrcamentoDAOImpl();
    FormaPagamentoDAOImpl daoFormaPagamento = new FormaPagamentoDAOImpl();
    PagamentoDAOImpl daoPagamento = new PagamentoDAOImpl();
    TipoProdutoDAOImpl daoTipoProduto = new TipoProdutoDAOImpl();
    Exceptions exceptions = new Exceptions();

    ProdutoOrcamento produtoOrcamento = new ProdutoOrcamento();
    List<ProdutoOrcamento> list = new ArrayList<ProdutoOrcamento>();
    float valor = 0;
    int indexTabela;
    boolean tipo = false;

    /**
     * Creates new form DialogCadOrcamento
     */
    public DialogCadOrcamento(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        //configurarTabela();
        this.setTitle("Orçamento");
        this.setLocationRelativeTo(null);
        carregarComboCliente();
    }

    private void carregarComboCliente() {
        try {

            if (tipo == false) {
                cbCliente.setModel(new ComboBoxClienteModel(daoCliente.buscaTodos()));
            }

            cbFormaPagamento.setModel(new ComboBoxFormaPagamentoModel(daoFormaPagamento.pesquisarTodosFormaPagamento()));

        } catch (Exception e) {
            exceptions.Exceptions(e.getMessage(), "DialogCadOrcamento-carregarComboCliente");
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cbCliente = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        txEndereco = new javax.swing.JTextField();
        txEndereco.setDocument(new LimitarTamanhoTextField(100));
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txCep = new javax.swing.JFormattedTextField();
        txBairro = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txTelefone = new javax.swing.JFormattedTextField();
        txCidade = new javax.swing.JTextField();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jXHyperlink2 = new org.jdesktop.swingx.JXHyperlink();
        jPanel2 = new javax.swing.JPanel();
        ckNotaFiscal = new javax.swing.JCheckBox();
        cbFormaPagamento = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        jXHyperlink1 = new org.jdesktop.swingx.JXHyperlink();
        jLabel3 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        lbValorTotalProdutos = new javax.swing.JLabel();
        lbValorNotaFiscal = new javax.swing.JLabel();
        lbValorTotal = new javax.swing.JLabel();
        jFormattedTextField1 = new javax.swing.JFormattedTextField();
        jPanel3 = new javax.swing.JPanel();
        btCancelar = new javax.swing.JButton();
        btRemover = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelaProduto = new javax.swing.JTable();
        btEditar = new javax.swing.JButton();
        btAdicionar = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        btSalvar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Informações cliente"));

        jLabel1.setText("Cliente:");

        cbCliente.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione um cliente" }));
        cbCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbClienteActionPerformed(evt);
            }
        });

        jLabel4.setText("Endereço:");

        txEndereco.setEnabled(false);
        txEndereco.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txEnderecoActionPerformed(evt);
            }
        });

        jLabel5.setText("Cidade:");

        jLabel6.setText("CEP:");

        txCep.setEnabled(false);

        txBairro.setDocument(new LimitarTamanhoTextField(70));
        txBairro.setEnabled(false);

        jLabel11.setText("UF:");

        jLabel9.setText("Telefone:");

        txTelefone.setEnabled(false);

        txCidade.setDocument(new LimitarTamanhoTextField(70));
        txCidade.setEnabled(false);

        jTextField1.setEnabled(false);

        jLabel2.setText("Bairro:");

        jXHyperlink2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/outros/Pesquisa.png"))); // NOI18N
        jXHyperlink2.setText("");
        jXHyperlink2.setToolTipText("Pesquisar");
        jXHyperlink2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jXHyperlink2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(txBairro, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jLabel2))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addComponent(jLabel11)
                                                .addGap(33, 33, 33))
                                            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE)))
                                    .addComponent(txCidade, javax.swing.GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(txTelefone, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)
                                    .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txCep, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(109, 109, 109))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(txEndereco, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cbCliente, javax.swing.GroupLayout.Alignment.LEADING, 0, 400, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jXHyperlink2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jXHyperlink2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(cbCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txCep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txCidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(jLabel11)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txBairro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Pagamento"));

        ckNotaFiscal.setText("Com nota fiscal.");
        ckNotaFiscal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ckNotaFiscalActionPerformed(evt);
            }
        });

        jLabel8.setText("Forma de pagamento:");

        jXHyperlink1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Add.png"))); // NOI18N
        jXHyperlink1.setText("");
        jXHyperlink1.setToolTipText("Adicionar nova forma de pagamento");
        jXHyperlink1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jXHyperlink1ActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setText("Valor total produtos:");

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel7.setText("Valor nota fiscal:");

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel10.setText("Valor Total:");

        lbValorTotalProdutos.setText("R$ 00,00");

        lbValorNotaFiscal.setText("0%");

        lbValorTotal.setText("R$ 00,00");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(cbFormaPagamento, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jXHyperlink1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(ckNotaFiscal)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jFormattedTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(60, 60, 60)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addGap(23, 23, 23)
                            .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel10)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbValorTotalProdutos, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lbValorNotaFiscal, javax.swing.GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE)
                    .addComponent(lbValorTotal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(54, 54, 54))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lbValorTotalProdutos, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lbValorNotaFiscal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lbValorTotal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap())
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jXHyperlink1, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE)
                            .addComponent(cbFormaPagamento, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(ckNotaFiscal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jFormattedTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(10, 10, 10))))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Produto"));

        btCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Cancel.png"))); // NOI18N
        btCancelar.setText("Cancelar");
        btCancelar.setEnabled(false);
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });

        btRemover.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Remove.png"))); // NOI18N
        btRemover.setText("Remover");
        btRemover.setEnabled(false);
        btRemover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRemoverActionPerformed(evt);
            }
        });

        tabelaProduto.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tabelaProduto.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelaProdutoMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabelaProduto);

        btEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Editar.png"))); // NOI18N
        btEditar.setText("Editar");
        btEditar.setEnabled(false);
        btEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEditarActionPerformed(evt);
            }
        });

        btAdicionar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Add.png"))); // NOI18N
        btAdicionar.setText("Adicionar");
        btAdicionar.setToolTipText("Adicionar material");
        btAdicionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAdicionarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGap(190, 190, 190)
                                .addComponent(btRemover)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btCancelar))
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(btAdicionar, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btEditar)))
                        .addGap(10, 10, 10))))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btAdicionar)
                    .addComponent(btEditar)
                    .addComponent(btRemover)
                    .addComponent(btCancelar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE)
                .addContainerGap())
        );

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Cancel.png"))); // NOI18N
        jButton1.setText("Cancelar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        btSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Save.png"))); // NOI18N
        btSalvar.setText("Salvar Orçamento");
        btSalvar.setEnabled(false);
        btSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 453, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(10, 10, 10)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btSalvar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(btSalvar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void configurarTabela(List<ProdutoOrcamento> lista) {
        try {

            this.setListaProdutoOrcamento(lista);

            tabelaProduto.setAutoCreateColumnsFromModel(false);
            java.awt.FontMetrics fm = tabelaProduto.getFontMetrics(tabelaProduto.getFont());

            tabelaProduto.setColumnModel(new ProdutoOrcamentoColumnModel(fm));
            tabelaProduto.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        } catch (Exception e) {
            exceptions.Exceptions(e.getMessage(), "DialogCadOrcamento-configurarTabela");
        }
    }

    public void setListaProdutoOrcamento(List<ProdutoOrcamento> listProduto) {
        this.tabelaProduto.setModel(new ProdutoOrcamentoTableModel(listProduto));
    }

    private void btAdicionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAdicionarActionPerformed
        DialogCadProdutoOrcamento cadastroProduto = new DialogCadProdutoOrcamento(null, true);

        if (produtoOrcamento.getTipoProduto() != null) {
            cadastroProduto.setProduto(produtoOrcamento);
        }
        cadastroProduto.setVisible(true);

        List<MaterialProdutoOrcamento> listaMaterialProduto = new ArrayList<MaterialProdutoOrcamento>();
        ProdutoOrcamento produto = new ProdutoOrcamento();

        listaMaterialProduto = cadastroProduto.retornoListaMaterialProduto();

        produto = cadastroProduto.retornoProdutoOrcamento();
        produto.setListaMaterial(listaMaterialProduto);

        if (listaMaterialProduto != null) {

            list.add(produto);
            configurarTabela(list);

            calcularValores(list);

            btSalvar.setEnabled(true);

        } else {
        }
}//GEN-LAST:event_btAdicionarActionPerformed

    private void btRemoverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRemoverActionPerformed
        list.remove(indexTabela);

        configurarTabela(list);

        lbValorNotaFiscal.setText("0%");
        lbValorTotal.setText("R$ 00,00");
        lbValorTotalProdutos.setText("R$ 00,00");
}//GEN-LAST:event_btRemoverActionPerformed

    private void txEnderecoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txEnderecoActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txEnderecoActionPerformed

    private void jXHyperlink2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jXHyperlink2ActionPerformed

    }//GEN-LAST:event_jXHyperlink2ActionPerformed

    private void jXHyperlink1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jXHyperlink1ActionPerformed
        DialogCadFormaPagamento cadForma = new DialogCadFormaPagamento(null, true);
        cadForma.setVisible(true);
        tipo = true;
        carregarComboCliente();
        tipo = false;
    }//GEN-LAST:event_jXHyperlink1ActionPerformed

    private void ckNotaFiscalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ckNotaFiscalActionPerformed

        NumberFormat nf = NumberFormat.getCurrencyInstance();

        if (ckNotaFiscal.isSelected() == true) {

            lbValorNotaFiscal.setText("+ 6%");
            float porcentagem = (float) (valor * 0.06);
            float total = porcentagem + valor;
            lbValorTotal.setText(nf.format(total));

        } else {

            lbValorNotaFiscal.setText("0%");
            lbValorTotal.setText(nf.format(valor));

        }
    }//GEN-LAST:event_ckNotaFiscalActionPerformed

    private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarActionPerformed
        try {

            DecimalFormat df = new DecimalFormat("0.000");

            if (cbCliente.getSelectedIndex() >= 0) {

                if (cbFormaPagamento.getSelectedIndex() >= 0) {

                    ComboBoxClienteModel model = (ComboBoxClienteModel) cbCliente.getModel();
                    ComboBoxFormaPagamentoModel modelForma = (ComboBoxFormaPagamentoModel) cbFormaPagamento.getModel();

                    Orcamento orcamento = new Orcamento();
                    orcamento.setCliente((Cliente) model.getSelectedItem());

                    Pagamento pagamento = new Pagamento();
                    pagamento.setFormaPagamento((FormaPagamento) modelForma.getSelectedItem());

                    String por = lbValorNotaFiscal.getText().replace("+", "").replace("%", "");

                    double vNota = Double.valueOf(por);
                    pagamento.setValorNotaFiscal(vNota);

                    String total = lbValorTotal.getText().replace("R$", "").replace(",", ".");
                    double vTotal = Double.valueOf(total);
                    pagamento.setValorTotal(vTotal);

                    String totalProduto = lbValorTotalProdutos.getText().replace("R$", "").replace(",", ".");
                    double vTotalProduto = Double.valueOf(totalProduto);
                    pagamento.setValorTotalProduto(vTotalProduto);

                    int idPagamento = daoPagamento.inserirPagamento(pagamento);

                    if (idPagamento != 0) {

                        pagamento.setIdPagamento(idPagamento);
                        orcamento.setPagamento(pagamento);

                        DateFormat t = new SimpleDateFormat("dd/MM/yyyy");
                        Date da = new Date();
                        String dateCad = t.format(da.getTime());

                        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
                        java.sql.Date dataCadastro = new java.sql.Date(formatter.parse(dateCad).getTime());
                        orcamento.setData(dataCadastro);

                        int idOrcamento = daoOrcamento.inserirOrcamento(orcamento);

                        int idProdutoOrcamento = 0;
                        if (idOrcamento != 0) {

                            for (ProdutoOrcamento produto : list) {

                                idProdutoOrcamento = daoProdutoOrcamento.inserirProdutoOrcamento(produto, idOrcamento);

                                if (idProdutoOrcamento != 0) {

                                    for (MaterialProdutoOrcamento materialProdutoOrcamento : produto.getListaMaterial()) {
                                        daoMaterialProduto.inserirMaterialProdutoOrcamento(materialProdutoOrcamento, idProdutoOrcamento);
                                    }
                                }
                            }
                        }

                        this.hide();

                    } else {
                        JOptionPane.showMessageDialog(null, "Erro ao inserir orçamento", "Orçamento", 1, new javax.swing.ImageIcon(getClass().getResource("/ima/erro/Erros.png")));
                        exceptions.Exceptions("Erro inserir orcamento", "DialogCadOrcamento-btSalvarActionPerformed");
                    }

                } else {
                    JOptionPane.showMessageDialog(null, "Selecione uma forma de pagamento", "Forma Pagamento", 1);
                }

            } else {
                JOptionPane.showMessageDialog(null, "Selecione um cliente", "Cliente", 1);
            }
        } catch (Exception e) {
            exceptions.Exceptions(e.getMessage(), "DialogCadOrcamento-btSalvarActionPerformed");
            JOptionPane.showMessageDialog(null, "Erro ao inserir orçamento", "Orçamento", 1);
        }
    }//GEN-LAST:event_btSalvarActionPerformed

    private void cbClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbClienteActionPerformed
        try {
            ComboBoxClienteModel model = (ComboBoxClienteModel) cbCliente.getModel();

            Cliente cliente = (Cliente) model.getSelectedItem();

//            txEndereco.setText(cliente.getEndereco());
//            txCidade.setText(cliente.getCidade());
//            //txCep.setText(cliente.getCep());
//            txBairro.setText(cliente.getBairro());
//            jTextField1.setText(cliente.getEstado());
            //txTelefone.setText(cliente.getTelefone());
        } catch (Exception e) {
            exceptions.Exceptions(e.getMessage(), "DialogCadOrcamento-cbClienteActionPerformed");
        }
    }//GEN-LAST:event_cbClienteActionPerformed

    private void tabelaProdutoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelaProdutoMouseClicked
        indexTabela = tabelaProduto.getSelectedRow();

        btAdicionar.setEnabled(false);
        btRemover.setEnabled(true);
        btEditar.setEnabled(true);
        btCancelar.setEnabled(true);

        btSalvar.setEnabled(false);
    }//GEN-LAST:event_tabelaProdutoMouseClicked

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
        btAdicionar.setEnabled(true);
        btRemover.setEnabled(false);
        btEditar.setEnabled(false);
        btCancelar.setEnabled(false);

        if (tabelaProduto.getRowCount() > 0) {
            btSalvar.setEnabled(true);
        }

    }//GEN-LAST:event_btCancelarActionPerformed

    private void btEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEditarActionPerformed
        DialogCadProdutoOrcamento cadProduto = new DialogCadProdutoOrcamento(null, true);
        cadProduto.setEditarProdtuoMaterial(list);
        cadProduto.setVisible(true);
    }//GEN-LAST:event_btEditarActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (list != null) {

            Object[] options = {"Sim", "Cancelar"};

            int i = JOptionPane.showOptionDialog(null,
                    "Você tem certeza do cancelamento do processo? Você perderá todas as infromações, deseja cancelar?", "Cancelar operação",
                    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
                    options, options[0]);

            if (i == JOptionPane.YES_OPTION) {

                list = null;
                this.hide();
            }

        }
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                DialogCadOrcamento dialog = new DialogCadOrcamento(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAdicionar;
    private javax.swing.JButton btCancelar;
    private javax.swing.JButton btEditar;
    private javax.swing.JButton btRemover;
    private javax.swing.JButton btSalvar;
    private javax.swing.JComboBox cbCliente;
    private javax.swing.JComboBox cbFormaPagamento;
    private javax.swing.JCheckBox ckNotaFiscal;
    private javax.swing.JButton jButton1;
    private javax.swing.JFormattedTextField jFormattedTextField1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    private org.jdesktop.swingx.JXHyperlink jXHyperlink1;
    private org.jdesktop.swingx.JXHyperlink jXHyperlink2;
    private javax.swing.JLabel lbValorNotaFiscal;
    private javax.swing.JLabel lbValorTotal;
    private javax.swing.JLabel lbValorTotalProdutos;
    private javax.swing.JTable tabelaProduto;
    private javax.swing.JTextField txBairro;
    private javax.swing.JFormattedTextField txCep;
    private javax.swing.JTextField txCidade;
    private javax.swing.JTextField txEndereco;
    private javax.swing.JFormattedTextField txTelefone;
    // End of variables declaration//GEN-END:variables

    private void calcularValores(List<ProdutoOrcamento> list) {
        try {

            NumberFormat nf = NumberFormat.getCurrencyInstance();

            for (ProdutoOrcamento p : list) {
                valor += p.getPesoTotal() * p.getValorAluminio();
            }

            lbValorTotalProdutos.setText(nf.format(valor));

            if (ckNotaFiscal.isSelected() == true) {
                float total = (float) (valor + 0.06);
                lbValorTotal.setText(nf.format(total));
            } else {
                lbValorTotal.setText(nf.format(valor));
            }

        } catch (Exception e) {
            exceptions.Exceptions(e.getMessage(), "DialogCadOrcamento-calcularValores");
        }
    }
}
